$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'psbranchcompany/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '分公司名称', name: 'companyName', index: 'company_name', width: 100 },
            { label: '联系人', name: 'contactPeople', index: 'contact_people', width: 80 },
            { label: '联系电话', name: 'contactNumber', index: 'contact_number', width: 80 },
            { label: '分公司营业执照', name: 'companyLicenceImg', index: 'company_licence_img', width: 80 },
			{ label: '分公司地址', name: 'address', index: 'address', width: 150 }

        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            companyName: null,
            contactPeople: null
        },
		showList: true,
		title: null,
		psBranchCompany: {},
        imageUrl:''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.psBranchCompany = {};
			vm.imageUrl = "";
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.psBranchCompany.id == null ? "psbranchcompany/save" : "psbranchcompany/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.psBranchCompany),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "psbranchcompany/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "psbranchcompany/info/"+id, function(r){
                var frontUrl = '/qwj_shop/fileDirect/backManageDriver/';
                vm.psBranchCompany = r.psBranchCompany;
                vm.imageUrl = frontUrl + vm.psBranchCompany.companyLicenceImg;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'companyName': vm.q.companyName,'contactPeople':vm.q.contactPeople},
                page:page
            }).trigger("reloadGrid");
		},
        handleAvatarSuccess: function(res, file) {
            vm.psBranchCompany.companyLicenceImg = res.url;
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload: function(file) {

            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                alert('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                alert('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
	}
});